local LuaMongo = _G.lua_mongo
local DB = require("common.DB")
local RankDefine = require("rank.RankDefine")
local Msg = require("core.Msg")
local RoleFFI  = require("role.RoleFFI")
local RoleDBLogic = require("role.RoleDBLogic")
local PfLogic = require("tencentApi.PfLogic")

PATA_RANK = PATA_RANK or {}
--PATA_RANK = {index = {uuid,name,pata_max,create=os.time()},index{uuid,name,pata_max}}

function setRankList(human, list, index)
  list.name = PATA_RANK[index].name
  list.uuid = PATA_RANK[index].uuid
  list.pata_max = PATA_RANK[index].pata_max
  list.sex = 0
  if PfLogic.isBlueShow(human) then
    list.blue = PATA_RANK[index].blue
  else
    list.blue = "0000"
  end
  list.args = 0
end

function GetRankList(human)
  local sendMsg = Msg.gc.GC_GET_PATA_RANK
  sendMsg.myRank = 0
  sendMsg.patalist[0] = #PATA_RANK
  for i=1, #PATA_RANK do
    if PATA_RANK[i].uuid == human._id then
      sendMsg.myRank = i
    end
    setRankList(human, sendMsg.patalist[i], i)
  end 
  Msg.send(sendMsg, human.fd)
end

function GetFirstRank(human)
  local sendMsg = Msg.gc.GC_PATA_FIRST_RANK
  if #PATA_RANK >= 1 then
    sendMsg.first[0] = 1
    setRankList(human, sendMsg.first[1], 1)
    local db = RoleDBLogic.getDbByUuid(PATA_RANK[1].uuid)
    sendMsg.first[1].sex = db.sex
  else
    sendMsg.first[0] = 0
  end  
  Msg.send(sendMsg, human.fd)
end

function setRankInfo(db, index)
  PATA_RANK[index] = {}
  PATA_RANK[index].uuid = RoleFFI.ffi_string(db._id)
  PATA_RANK[index].name = RoleFFI.ffi_string(db.name)
  PATA_RANK[index].pata_max = db.pata_max
  PATA_RANK[index].create = os.time()
  PATA_RANK[index].blue = string.format("%d%d%d", db.blueVipLv, db.blueYearVip, db.blueSuperVip, db.level3366)
end

function sortFunc(a, b)
  if a.pata_max == b.pata_max then
    return a.create < b.create
  end
  return a.pata_max > b.pata_max
end

function isInRank(db)
  for i=1, #PATA_RANK do
    if PATA_RANK[i].uuid == RoleFFI.ffi_string(db._id) then
      if PATA_RANK[i].pata_max ~= db.pata_max then
        setRankInfo(db, i)
        table.sort(PATA_RANK, sortFunc)
      end
      return true
    end
  end
  return false
end
--外部调用，等级变更的时候调用
function PataChange(human, db)
  if not db then
   db = human.db
  end
  if db.lv < 35 then return end
  if isInRank(db) then
    return
  end
  if #PATA_RANK < RankDefine.RANK_MAX_CNT then
    setRankInfo(db, #PATA_RANK + 1)
  elseif #PATA_RANK >= RankDefine.RANK_MAX_CNT and db.pata_max > PATA_RANK[#PATA_RANK].pata_max then
    setRankInfo(db, RankDefine.RANK_MAX_CNT)
  else
    return false
  end  
  table.sort(PATA_RANK, sortFunc)
end

function getRank()
  local dbType = {type = RankDefine.PATA_RANK_TYPE}
  LuaMongo.find(DB.db_rank, dbType)
  while true do
    local tmp = {}
    if not LuaMongo.next(tmp) then
      break
    end
    PATA_RANK[tmp.index] = {}
    PATA_RANK[tmp.index].uuid = tmp.uuid
    PATA_RANK[tmp.index].name = tmp.name
    PATA_RANK[tmp.index].pata_max = tmp.pata_max
    PATA_RANK[tmp.index].create = tmp.create or os.time()
    PATA_RANK[tmp.index].blue = tmp.blue or "0000"
  end
end

function wrapperData(index)
  local db = {}
  db.type = RankDefine.PATA_RANK_TYPE
  db.index = index
  db.uuid = PATA_RANK[index].uuid
  db.name = PATA_RANK[index].name
  db.pata_max = PATA_RANK[index].pata_max
  db.create = PATA_RANK[index].create or os.time()
  db.blue = PATA_RANK[index].blue or "0000"
  return db
end

function saveRank()
  for i=1, #PATA_RANK do
    local db = wrapperData(i)
    LuaMongo.insert(DB.db_rank, db)
  end
end